
TOOL.Category		= "PC Mod - Hardware"
TOOL.Name			= "#Laptop"
TOOL.Tab			= "PC Mod"
TOOL.Command		= nil
TOOL.ConfigName		= ""

TOOL.ClientConVar[ "weld" ] = 1
TOOL.ClientConVar[ "password" ] = ""
TOOL.ClientConVar[ "wireless" ] = 0
TOOL.ClientConVar[ "model" ] = "models/laptop/laptop.mdl"

cleanup.Register( "computers" )

if (CLIENT) then
	language.Add( 'Tool_laptop_name', 'Laptop Spawner' )
	language.Add( 'Tool_laptop_desc', 'Create laptops for use with a PC network' )
	language.Add( 'Tool_laptop_0', 'Left-Click: Spawn a laptop.' )
	
	language.Add( 'Undone_laptop', 'Laptop Undone' )
	language.Add( 'Cleanup_computers', 'Computers' )
	language.Add( 'Cleaned_computers', 'Cleaned up all computers' )
end

function TOOL:LeftClick( trace )

	// If the target is a player, stop here.
	if ( trace.Entity && trace.Entity:IsPlayer() ) then return false end
	
	// The rest of the function is server-only stuff.
	if (CLIENT) then return true end
	
	// Get some variables
	local ply = self:GetOwner()
	local weld = ( self:GetClientNumber( "weld" ) == 1 )
	local wireless = ( self:GetClientNumber( "wireless" ) == 1 )
	local Ang = trace.HitNormal:Angle()
	Ang.pitch = Ang.pitch + 90
	local Pos = trace.HitPos

	// Make the entity
	local ent = MakePC( ply, Ang, Pos )
	local const
	
	// If the entity does not exist, end here
	if (!ent) then return false end
	
	ent.Password = self:GetClientInfo( "password" )
	ent:MakeLaptop( wireless )
	
	// Only weld if we want to
	if ( weld ) and ( trace.Entity ) and (!trace.HitWorld) then
		const = constraint.Weld( ent, trace.Entity, 0, trace.PhysicsBone, 0, collision == 0, true )
	end
	
	// With thanks to WireMod team :P
	local min = ent:OBBMins()
	ent:SetPos( trace.HitPos - trace.HitNormal * min.z )
	
	// Add this to the undo list
	undo.Create("PC")
		undo.AddEntity( ent )
		undo.AddEntity( const )
		undo.SetPlayer( ply )
	undo.Finish()
	
	// Add this to the cleanup list
	ply:AddCleanup( "computers", ent )
	ply:AddCleanup( "computers", const )
	
	// This was successful!
	return true
end

if (SERVER) then

	function MakePC( pl, Ang, Pos )
	
		// Create the entity and stop here if it isn't valid
		local pc = ents.Create( "pcmod_pc" )
		if (!pc:IsValid()) then return false end

		// Set it's angles and position, and spawn it
		pc:SetAngles( Ang )
		pc:SetPos( Pos )
		pc:Spawn()

		// Add it to the counter
		pl:AddCount( "computers", pc )
		
		// Make the effect
		DoPropSpawnedEffect( pc )

		// Return the entity
		return pc
		
	end

end

function TOOL:UpdateGhost( ent, player )

	if ( !ent || !ent:IsValid() ) then return end

	local tr 	= utilx.GetPlayerTrace( player, player:GetCursorAimVector() )
	local trace 	= util.TraceLine( tr )
	
	if (!trace.Hit || trace.Entity:IsPlayer() || trace.Entity:GetClass() == "pcmod_pc" ) then
		ent:SetNoDraw( true )
		return
	end
	
	local Ang = trace.HitNormal:Angle()
	Ang.pitch = Ang.pitch + 90
	ent:SetAngles( Ang )	

	local min = ent:OBBMins()
	ent:SetPos( trace.HitPos - trace.HitNormal * min.z )
	
	ent:SetNoDraw( false )

end

function TOOL:Think()
	if (!self.GhostEntity || !self.GhostEntity:IsValid() || self.GhostEntity:GetModel() != self:GetClientInfo( "model" )) then
		self:MakeGhostEntity( self:GetClientInfo( "model" ), Vector(0,0,0), Angle(0,0,0) )
	end
	
	self:UpdateGhost( self.GhostEntity, self:GetOwner() )
end

function TOOL.BuildCPanel( CPanel )

	pc_maketoolinfo( CPanel:AddControl( "Label", { Text = "", Description = "" } ), "Laptop", "Acts like a PC, but with wireless." )

	// HEADER
	CPanel:AddControl( "Header", { Text = "#Tool_laptop_name", Description	= "#Tool_laptop_desc" }  )

	// BODY
	CPanel:AddControl( "CheckBox", { Label = "Weld:",
									 Command = "laptop_weld" } )
									 
	CPanel:AddControl( "Textbox", { Label = "Password:",
									MaxLength = 16,
									Command = "laptop_password" } )

	CPanel:AddControl( "CheckBox", { Label = "Wireless",
									 Command = "laptop_wireless" } )									

end